<template>
  <a-select
    show-search
    option-filter-prop="children"
    :placeholder="$t('pleaseSelect') + $t('role').toLowerCase()"
    :filter-option="filterOption"
    :allow-clear="true"
    :mode="mode"
    :maxTagCount="10"
    v-model="selectedValue"
    @change="changeOption"
  >
    <a-select-option v-for="item in options" :key="item.id" :value="item.id" :title="item.name">
      {{ item.name }}
    </a-select-option>
  </a-select>
</template>
<script>
import { mapState } from 'vuex'
export default {
  props: {
    defaultValue: {
      type: [Array, String, Object],
      default () {
        return []
      }
    },
    mode: {
      type: String,
      default: 'multiple'
    }
  },
  computed: {
    ...mapState({
      options: (state) => state.role.roleData
    })
  },
  data () {
    return {
      selectedValue: []
    }
  },
  watch: {
    defaultValue: {
      handler (val) {
        if (val !== this.selectedValue) {
          this.selectedValue = this.defaultValue
        }
      },
      immediate: true
    }
  },
  created () {
    this.getData()
  },
  methods: {
    getData () {
      this.$store.dispatch('getRoleData')
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    changeOption (val) {
      this.$emit('changerole', val)
    }
  }
}
</script>
